package com.dqhl.business.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.business.Base.BaseDialogActivity;
import com.dqhl.business.R;
import com.dqhl.business.adapter.AddPromotionSingleGridViewAdapter;
import com.dqhl.business.modle.AddPromotion;
import com.dqhl.business.modle.GoodsType;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.Dlog;
import com.dqhl.business.util.JsonUtils;
import com.dqhl.business.view.MyGridView;
import com.dqhl.business.view.PromotionCouponPopupWindow;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * description: 单品促销添加指定商品
 * autour: guanluocang
 * date: 2016/11/8 20:41
 */

public class AddOnlyGoodsDialogActivity extends BaseDialogActivity implements View.OnClickListener {

    private ImageView ic_cancle;
    private TextView tv_goodsType;
    private MyGridView gv_singleGoods;
    private List<GoodsType> goodsTypeList;
    private List<AddPromotion> addPromotionList;
    private AddPromotionSingleGridViewAdapter mAddPromotionSingleGridViewAdapter;
    private PromotionCouponPopupWindow mPromotionCouponPopupWindow;
    private static final int RESULTCODE = 101;

    private ImageView iv_yes;
    private String type_id;
    private String goods_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_only_goods_dialog);
        initView();
        initData();
    }

    private void initData() {

    }

    private void initView() {
        ic_cancle = (ImageView) findViewById(R.id.ic_cancle);
        tv_goodsType = (TextView) findViewById(R.id.tv_goodsType);
        gv_singleGoods = (MyGridView) findViewById(R.id.gv_singleGoods);
        iv_yes = (ImageView) findViewById(R.id.iv_yes);

        iv_yes.setOnClickListener(this);
        tv_goodsType.setOnClickListener(this);
        ic_cancle.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ic_cancle:
                finish();
                break;
            //选择商品
            case R.id.tv_goodsType:
                showStatusPopWindow();
                break;
            //确定
            case R.id.iv_yes:
                Intent intent = new Intent();
                intent.putExtra("goods_id", goods_id);
                setResult(RESULTCODE, intent);
                finish();
                break;
        }
    }

    //选择商品popwindow
    private void showStatusPopWindow() {
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.management_goods_type_list);
        params.addBodyParameter("seller_id", user.getUser_id());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e("选择商品" + result);
                int errCode = JsonUtils.getErrCode(result);
                String errMsg = JsonUtils.getErrMsg(result);
                String data = JsonUtils.getData(result);
                if (0 == errCode) {
                    goodsTypeList = JSON.parseArray(data, GoodsType.class);
                    mPromotionCouponPopupWindow = new PromotionCouponPopupWindow(AddOnlyGoodsDialogActivity.this, goodsTypeList,
                            new PromotionCouponPopupWindow.IOnItemSelectListener() {
                                @Override
                                public void onItemClick(int position) {
                                    tv_goodsType.setText(goodsTypeList.get(position).getName());
                                    type_id = goodsTypeList.get(position).getId()+"";
                                    getGoods();
                                }
                            });
                    mPromotionCouponPopupWindow.setWidth(tv_goodsType.getWidth());
                    mPromotionCouponPopupWindow.showAsDropDown(tv_goodsType);


                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    //商品列表
    public void getGoods() {
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.management_goods_type_goods_list);
        params.addBodyParameter("seller_id", user.getUser_id());
        params.addBodyParameter("class_id", type_id);
        params.addBodyParameter("open", "1");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e("GGDialogActivity" + result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");
                if (errCode == 0) {
                    addPromotionList = JSON.parseArray(data, AddPromotion.class);
                    mAddPromotionSingleGridViewAdapter = new AddPromotionSingleGridViewAdapter(AddOnlyGoodsDialogActivity.this, addPromotionList);
                    gv_singleGoods.setAdapter(mAddPromotionSingleGridViewAdapter);
                    gv_singleGoods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            goods_id = addPromotionList.get(i).getId()+"";
                            mAddPromotionSingleGridViewAdapter.clearSelection(i);
                            mAddPromotionSingleGridViewAdapter.notifyDataSetChanged();
                        }
                    });
                } else {
                    toast("暂无数据");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });

    }
}
